
var call_div_timer = null;

if(document.getElementById('this_is_hzcf_crm'))
{
	$('body').attr('pla',1);
	var btn_call =  $('#call-btn-call');
	var btn_transfer = '';
	var btn_clear = '';
	var btn_listen = '';
	var call_div = $('#call-div');

	$('#btn-mute').click( function(){
		var  _callData = {};
		_callData.op = 'mute';
		hzcf_om_proxy( _callData );
	});

	btn_call.click( function(){
		var _phone = $('#call-input-number').val().trim();
		if( !_phone || _phone.length < 10){
			alert("拨打电话号码格式错误");
			return false;
		}else{
			if( call_div_timer )
				clearTimeout( call_div_timer );
			var _callData = {};
			_callData.op = 'dail';
			_callData.data = _phone;
			hzcf_om_proxy( _callData );
		}
	});

	$('#call-btn-hangup').click( function(){
		clearTimeout( call_div_timer );
		var  _callData = {};
		_callData.op = 'clear';
		if( $(this).attr('vid') ){
			_callData.vid = $(this).attr('vid');
			$(this).attr('vid','');
		}
		else if( $(this).attr('oid') ){
			_callData.oid = $(this).attr('oid');
			$(this).attr('oid','');
		}
		hzcf_om_proxy( _callData );
		$("#call-sub-call").show();
		$('#call-btn-call').show();
		$('#call-btn-transfer').hide();
		$('#call-btn-hangup').hide();
		$('#hzcf_console').html("通话已经挂断");
		hzcf_load_uinfo('');
	});
	hzcf_om_connect();
}

function hzcf_om_proxy( _data ){
	var hzcf_console = $('#hzcf_console');
	chrome.extension.sendRequest({om_notify:1,data:_data}, function(response) {
		if( response && response.rcode ){
			hzcf_console.html("请求发送成功");
		}else{
			hzcf_console.html("请求发送失败");
		}
	});
}

function hzcf_om_transfer( _visitorID ){
	if( !_visitorID )
		return false;
	clearTimeout( call_div_timer );
	var _transfer = {};
	_transfer.op = 'transfer';
	_transfer.ext = _visitorID;
	_transfer.vid = $('#call-btn-hangup').attr('vid');
	$('#call-btn-hangup').attr('vid','');
	hzcf_om_proxy( _transfer );
	$('#hzcf_console').html("来电已经转接");
	$("#call-sub-call").show();
	$('#call-btn-call').show();
	$('#call-btn-transfer').hide();
	$('#call-btn-hangup').hide();
	$('#call-tansfer-list').html('');
}

function hzcf_om_connect(){
	var hzcf_console = $('#hzcf_console');
	hzcf_console.html("");
	var _uid = $('#hzcf_uid').val();
	var _omid = $('#hzcf_omid').val();
	var _extid = $('#hzcf_extid').val();
	var _addr  = $('#hzcf_addr').val();
	if( _uid && _omid && _extid && _addr){
		hzcf_console.html("连接到电话服务器");
		chrome.extension.sendRequest({addr:_addr,omID:_omid,extID:_extid,uid:_uid}, function(response) {
			if( !response || (response && !response.msg) ){
				hzcf_console.html("分机连接电话服务器失败");
			}else{
				hzcf_console.html("分机连接成功");
				if( response.line )
					$('#sp-line').html( response.line );
			}
		});
	}else{
		hzcf_console.html('页面参数设置错误,无法连接到服务器');
	}
}

function hzcf_ts_time(){
	var _l = parseInt( $('#call-sub-time').html() );
	_l++;
	$('#call-sub-time').html( _l );
	call_div_timer = setTimeout(function(){ hzcf_ts_time();}, 1000);
}

function hzcf_load_uinfo( _phone ){
	$('#hzcf_u_loadinfo').html('');
	if( !_phone )
		return false;
	  	$.ajax({
        url:  $('#hzcf_u_loadinfo').attr('url'),
        type: "POST",
        data:{op:'loaduinfo',phone:_phone},
        dataType:'json',
        success:function( d) {
		  	if( parseInt( d.rcode ) > 0){
				$('#hzcf_u_loadinfo').html( d.data );
		   	}
		  	else  {  $('#hzcf_u_loadinfo').html(d.msg );}
		 	},
        error:function(er){
          $('#hzcf_u_loadinfo').html('加载用户失败');
        }
     });
} 

chrome.extension.onRequest.addListener(
	function(request, sender, sendResponse) {
		var hzcf_console = $('#hzcf_console');
		/* 接收到background.js推送消息  */
		if( request.op && request.hzcf ){
			if( 'init' == request.op ){ 
				$('#sp-line').html( request.data.lineid );
			}
			else if( 'dail' == request.op){
				$('#call-sub-info').html( request.area + '  ' + $('#call-input-number').val() + " 通话时长： " );
				$('#call-sub-info').show();
				if( !request.data ){
					hzcf_console.html('拨号失败');
				}else{
					hzcf_load_uinfo( $('#call-input-number').val()  );
					//拨号计时
					$('#call-sub-call').hide();
					$('#call-btn-call').hide();
					$('#call-btn-transfer').hide();
					$('#call-btn-hangup').show();
					$('#call-sub-time').html( '0');
					$('#call-sub-time').show();
					hzcf_console.html( '正在拨号中，等待对方应答' );
					//setTimeout(function(){ hzcf_ts_time();}, 1000);
				}
			}
			else if( 'incoming' == request.op ){
				clearTimeout( call_div_timer );
				hzcf_load_uinfo( request.data['from'] );
				$('#call-sub-info').html( '来电信息 ' + request.data['area'] + '  ' +  request.data['from']+ " 通话时长： " );
				$('#call-sub-info').show();
				$("#call-sub-call").hide();
				var _html = '';
				var i = 0;
				var len = request.exts.length; 
				for(;i<len;i++){
					_html += '<li ext="'+request.exts[i]+'"><a href="#">'+request.exts[i]+"</a></li>";
				}
				$('#call-tansfer-list').html('');
				$('#call-tansfer-list').append( _html );
				$('#call-tansfer-list li').click(function(){
					hzcf_om_transfer( $(this).attr('ext') );
				});
				$('#call-btn-transfer').show();
				$('#call-btn-hangup').attr('vid', request.data['id']);
				$('#call-btn-hangup').show();
				$('#call-sub-call').hide();
				$('#call-btn-call').hide();
			}
			else if('cdr' == request.op ){ /*未接电话也是在这里判断*/
				$('#call-input-number').val('');
				hzcf_console.html( '通话结束' );
				setTimeout(function(){ $('#call-sub-info').html('');$('#call-sub-time').hide();}, 3000);
				if( $("#call-sub-call").is(":hidden") ){
					$("#call-sub-call").show();
					$('#call-btn-call').show();
					$('#call-btn-transfer').hide();
					$('#call-btn-hangup').hide();
					hzcf_load_uinfo('');
				}
				clearTimeout( call_div_timer );
				if( request.data ){
					var type = 'three';
					if( 0 == request.data.m_duration && ('IN' == request.data.m_type || 'FI' == request.data.m_type) ){
						type = 'one';
						var cnt = parseInt( $('#btn-view-call1 span').html() );
						cnt++;
						$('#btn-view-call1 span').html( cnt );
					}
					else if( 'OU' == request.data.m_type || 'FW' == request.data.m_type ){
						type = 'two';
						var cnt = parseInt( $('#btn-view-call2 span').html() );
						cnt++;
						$('#btn-view-call2 span').html( cnt );
					}
					else{
						$('#call_three_cnt').prepend( _tr );
						var cnt = parseInt( $('#btn-view-call3 span').html() );
						cnt++;
						$('#btn-view-call3 span').html( cnt );
					}

					//<span class="badge badge-important">0</span></a>
					//
					var _tr = '<tr><td>';
					if( 'one' == type)
						_tr += '<span class="label label-important">&nbsp;</span>';
					else if( 'two' == type )
						_tr += '<span class="label">&nbsp;</span>';
					else 
						_tr += '<span class="label label-success">&nbsp;</span>';
					_tr += request.data.tm+'</td><td>';
					_tr += request.data.m_extid == request.data.m_cdpn ? request.data.m_cpn : request.data.m_cdpn;
					_tr += '</td><td>' + request.data.m_duration + '</td><td>';
					_tr += request.data.area + '</td></tr>';
					$('#call_'+type+'_cnt').prepend( _tr );
				}
			}
			else if('bye' == request.op){
				hzcf_console.html( '通话结束' );
				$("#call-sub-call").show();
				$('#call-btn-call').show();
				$('#call-btn-transfer').hide();
				$('#call-btn-hangup').hide();
				clearTimeout( call_div_timer );
				hzcf_load_uinfo('');
			}
			else if( 'answer' == request.op || 'answered'== request.op ){
				hzcf_console.html( '通话开始计时' );
				$('#call-btn-call').hide();
				call_div_timer = setTimeout(function(){ hzcf_ts_time();}, 1000);
			}
            else if('offline' == request.op){
            	hzcf_console.html("分机掉线. 提示信息:" + request.msg );
            	if( $("#call-sub-call").is(":hidden") ){
					$("#call-sub-call").show();
					$('#call-btn-call').show();
					$('#call-btn-transfer').hide();
					$('#call-btn-hangup').hide();
				}
            	clearTimeout( call_div_timer );
            }
            else if('extchange' == request.op ){
                //
            } 
		}
		/* 接收到前端页面推送消息 */
	}
);